package com.itic.appbase.framework.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;  
import org.apache.commons.codec.binary.Base64;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESUtil {
	
	/* public static  String Encrypt(String str) {  
        try{  
            String key = "ONxYDyNaCoyTzsp83JoQ3YYuMPHxk3j7"; //密钥  
            byte[] kb = key.getBytes("utf-8");  
            SecretKeySpec sks = new SecretKeySpec(kb, "AES");  
            Cipher cipher = Cipher.getInstance("AES");//算法/模式/补码方式  
            cipher.init(Cipher.ENCRYPT_MODE, sks);  
            byte[] eb = cipher.doFinal(str.getBytes("utf-8"));  
            return new Base64().encodeToString(eb);  
        }catch(Exception e){  
            e.printStackTrace();  
            return null;  
        }  
	 }  
	      
    //解密  
    public static  String Decrypt(String str) {  
        try{  
            String key = "ONxYDyNaCoyTzsp83JoQ3YYuMPHxk3j7"; //密钥  
            byte[] kb = key.getBytes("utf-8");  
            SecretKeySpec sks = new SecretKeySpec(kb, "AES");  
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
            cipher.init(Cipher.ENCRYPT_MODE, sks);  
            byte[] by = new Base64().decode(str);  
            byte[] db = cipher.doFinal(by);  
            return new String(db);  
        }catch(Exception e){  
            e.printStackTrace();  
            return null;  
        }  
    }  */
	
	public static String encrypt(String encData ,String secretKey,String vector) throws Exception {
        if(secretKey == null) {
            return null;
        }
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] raw = secretKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
        return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码。
    }
	      
	public  static String decrypt(String sSrc,String key,String ivs) throws Exception {
        try {
            byte[] raw = key.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }
	public static void main(String[] args) throws Exception{  
		String encData = "{\"TimeStamp\":\"2017-01-12 14:58:45\",\"GrantType\":\"auth_token\",\"AuthParam\":\"xwz1CqvgxJiJVx7xhtKFSiDEdmojCYHA\"}";
		String secretKey = "ONxYDyNaCoyTzsp83JoQ3YYuMPHxk3j7";
		String vector = "yNaCoyTzsp83JoQ3";
        String en = AESUtil.encrypt(encData,secretKey,vector);  
        System.out.println("加密:"+en);  
          
        String de = AESUtil.decrypt(en,secretKey,vector);  
        System.out.println("解密:"+de);  
    }  

}
